﻿using System.Windows;
using System.Windows.Controls;
using Gao.Poker.App.Print;
using Gao.Poker.App.ViewModel;
using Gao.Poker.Services;
using Member = Gao.Poker.Domain.Member;
using MessageBox = Xceed.Wpf.Toolkit.MessageBox;

namespace Gao.Poker.App.Members
{
    /// <summary>
    /// Interaction logic for MemberMain.xaml
    /// </summary>
    public partial class MemberMain : UserControl
    {
        private MemberViewModel _viewModel;

        private MemberEdit memberEdit;
        private MemberView memberView;

        private static MemberMain _instance;
        MemberService ms;

        public static MemberMain Instance()
        {
            return new MemberMain();
        }

        public MemberMain()
        {
            //ms = new MemberService();
           // MemberList = new MemberResult();
            InitializeComponent();
            _viewModel = new MemberViewModel(cc);
            _viewModel.Message = OnMessage;
            DataContext = _viewModel;

            btnSearch.Click += Search_Click;
            memberEdit = new MemberEdit();
            memberView = new MemberView();

            cc.Content = memberView;
        }

        private void Search_Click(object sender, RoutedEventArgs e)
        {
            _viewModel.PageIndex = 0;
            //_viewModel.OnPropertyChanged("Members");
        }

        private void OnMessage(string obj)
        {
            MessageBox.Show(obj, "提示消息", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void Recharge_Click(object sender, RoutedEventArgs e)
        {
            Member member = dgMembers.SelectedItem as Member;
            if (member == null)
            {
                OnMessage("当前需要充值的会员不能为空！");
                return;
            }

            RechargeWin win = new RechargeWin();
            win.ViewModel = _viewModel;
            win.Member = _viewModel.Member;
            win.Show();
        }

        private void New_Click(object sender, RoutedEventArgs e)
        {
            cc.Content = memberEdit;
            _viewModel.Member = new Member();
        }

        private void Modifiy_Click(object sender, RoutedEventArgs e)
        {
            Member member = dgMembers.SelectedItem as Member;
            _viewModel.Member = member;
            if (member == null)
                OnMessage("当前并未选择会员资料！");
            else
            {
                cc.Content = memberEdit;
            }
        }

        private void dgMembers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            cc.Content = memberView;
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            Member member = dgMembers.SelectedItem as Member;
            if (member == null)
                OnMessage("当前并未选择会员资料！");
            else
            {
                var result = MessageBox.Show("确定停止使用当前会员卡", "提示消息", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                if (result == MessageBoxResult.Yes)
                {
                    _viewModel.Member.State = 1;
                    MemberService ms = new MemberService();
                    ms.Save(_viewModel.Member);

                    _viewModel.OnPropertyChanged("Members");
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
        }

        private void Pager_PageIndexChanging(int pageIndex, System.EventArgs e)
        {
            _viewModel.PageIndex = pageIndex;
        }

        //private void dataPager_PageChanged(object sender, CustomControlLibrary.PageChangedEventArgs args)
        //{
        //    Query(args.PageSize, args.PageIndex);
        //}
    }
}
